package com.lovo.pis.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lovo.pis.constant.MessageCode;
import com.lovo.pis.dto.PageBean;
import com.lovo.pis.dto.UserBean;
import com.lovo.pis.service.IUserService;
import com.lovo.pis.utils.JsonResult;
import com.lovo.pis.utils.SMSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;

    //用户名模糊分页查询所有
    @RequestMapping("/queryAll")
    public IPage<UserBean> queryAllByItem(@RequestBody PageBean<UserBean> pageInfo){
        return userService.queryAll(pageInfo);
    }

    //查询所有用户
    @RequestMapping("/queryAllUser")
    public List<UserBean> queryAllUser(){
        return userService.queryAllUser();
    }

    //通过id查询用户角色
    @RequestMapping("/query")
    public UserBean queryById(int id){
        return userService.queryById(id);
    }
    //通过id查询用户真名
    @RequestMapping("/queryRealName")
    public UserBean queryRealName(int id){
        return userService.queryRealName(id);
    }

    //删除用户
    @RequestMapping("/remove")
    public JsonResult removeById(int id){
        JsonResult jsonResult = new JsonResult();
        userService.removeById(id);
        return jsonResult;
    }

    //添加新用户
    @RequestMapping("/add")
    public JsonResult addUser(@RequestBody UserBean userBean){
        JsonResult jsonResult = new JsonResult();
        userService.addUser(userBean);
        return jsonResult;
    }

    //修改权限
    @RequestMapping("/update")
    public JsonResult updateRole(@RequestBody UserBean userBean){
        JsonResult jsonResult = new JsonResult();
        userService.changeRole(userBean);
        return jsonResult;
    }

    //修改密码
    @RequestMapping("/changePwd")
    public int changePwd(int id,String oldPwd,String newPwd,String repNewPwd){
        return userService.changePwd(id,oldPwd,newPwd,repNewPwd);
    }

    //登录触发
    @RequestMapping("/loginLog")
    public JsonResult<UserBean> login(HttpServletResponse response, String username, String password){
            JsonResult<UserBean> jsonResult = new JsonResult<>();
            UserBean userBean = userService.login(response,username,password);
            if (userBean==null){
                jsonResult.setCode(MessageCode.ACCOUNT_OR_PASSWORD_ERROR.getCode());
                jsonResult.setMessage(MessageCode.ACCOUNT_OR_PASSWORD_ERROR.getMessage());
            }
            jsonResult.setData(userBean);
        return jsonResult;
    }

    //随机验证码
    @RequestMapping("/checkCode")
    public char[] login(@RequestBody List<Integer> ints){
        char[] chars = new char[4];
        for (int i = 0; i < chars.length; i++) {
            chars[i]=((char)(int)ints.get(i));
        }
        return chars;
    }
    //随机验证码
    @RequestMapping("/phoneCode")
    public JsonResult<String> login(String phoneNumber) throws Exception {
        JsonResult<String> jsonResult = new JsonResult<>();
        jsonResult.setData(SMSUtil.sendPhoneCheckCode(SMSUtil.generateValidateCode(4),phoneNumber));
        return jsonResult;
    }



}
